package com.xindaoapp.happypet.ui;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.inputmethod.InputMethodManager;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.xindaoapp.happypet.R;
import com.xindaoapp.happypet.application.HappyPetApplication;
import com.xindaoapp.happypet.protocol.MoccaApi;
import com.xindaoapp.happypet.utils.CommonUtil;

public abstract class BaseFragment extends Fragment {
    protected View mView;
    protected Context mContext;
    protected LayoutInflater mInflater;
    /** 播放加载时动画 */
    protected FrameLayout loading;
    protected ImageView imageView;
    protected AnimationDrawable animation;
    protected View load_fail;

    protected String mResult = "0";

    private boolean isFirstLoading = true;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mContext = getActivity();
        mInflater = LayoutInflater.from(mContext);
        initViews();
        initEvents();
        onLoadDatas();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return mView;
    }

    protected void initViews() {

    }

    protected void initEvents() {}

    private void onLoadDatas() {

        loading = (FrameLayout) getView().findViewById(R.id.loading);
        load_fail = getView().findViewById(R.id.ll_load_fail);

        // 重试
        if (load_fail != null) {
            load_fail.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    onLoadDatas();
                }
            });
        }

        imageView = (ImageView) getView().findViewById(R.id.imageView);
        // if (imageView != null) {
        // imageView.setBackgroundResource(R.anim.alertdialog_animation);
        // animation = (AnimationDrawable) imageView.getBackground();
        // animation.setOneShot(false);
        // animation.start();
        // }
        // startAnimation();
        // loadDatas();
        if (imageView != null) {
            imageView.setBackgroundResource(R.anim.alertdialog_animation);
            animation = (AnimationDrawable) imageView.getBackground();
            animation.setOneShot(false);
            // animation.start();
            startAnimation();
        }
        if (CommonUtil.isNetworkAvailable(mContext) == 0) {
            onDataArrived(false);
        }
        loadDatas();
    }

    public void startAnimation() {
        if (loading.getVisibility() == View.GONE) {
            loading.setVisibility(View.VISIBLE);
        }
        if (load_fail.getVisibility() == View.VISIBLE) {
            load_fail.setVisibility(View.GONE);
        }
        if (animation != null && null != imageView) {
            animation.start();
        }
    }

    /** 隐藏键盘 */
    protected final void hideKeyBoard() {
        InputMethodManager inputManager =
                (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(getActivity().getWindow().getDecorView()
                .getWindowToken(), 0);
    }

    protected final void onDataArrived(boolean isArrived) {
        if (isArrived) {
            isFirstLoading = false;
            if (animation != null && null != imageView) {
                animation.stop();
                if (load_fail.getVisibility() == View.VISIBLE) {
                    load_fail.setVisibility(View.GONE);
                }
                loading.setVisibility(View.GONE);
            }
        } else {
            if (!isFirstLoading) {
                // Toast.makeText(getActivity(), R.string.net_error, Toast.LENGTH_SHORT).show();
                animation.stop();
                if (load_fail.getVisibility() == View.VISIBLE) {
                    load_fail.setVisibility(View.GONE);
                }
                loading.setVisibility(View.GONE);
            } else if (animation != null && null != imageView) {
                animation.stop();
                if (loading.getVisibility() == View.GONE) {
                    loading.setVisibility(View.VISIBLE);
                }
                if (load_fail.getVisibility() == View.GONE && isFirstLoading) {
                    load_fail.setVisibility(View.VISIBLE);
                }
            }
        }
    }

    /**
     * 请求成功，但是数据为空，页面显示暂无数据
     */
    protected final void onDataArrivedEmpty() {
        View view = null;
        try {
            view = getView().findViewById(R.id.no_data);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (null != loading && null != view) {
            loading.setVisibility(View.VISIBLE);
            view.setVisibility(View.VISIBLE);
            view.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub

                }
            });
        }
    }

    protected void loadDatas() {

    }

    protected MoccaApi getMoccaApi() {
        return HappyPetApplication.get().getMoccaApi();
    }

}
